package com.example.hystrixDemo.service.observable.imp;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixObservableCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import rx.Observable;
import rx.Observer;
import rx.Scheduler;
import rx.Subscriber;
import rx.observables.SyncOnSubscribe;
import rx.schedulers.Schedulers;

/**
 *@Deacription TODO
 *@Author baohuajie
 *@Date 2020/9/8 13:40
 *@Version 1.0
 **/
public class UserInfoServiceCommand extends HystrixObservableCommand<String>{

    private final Logger logger = LoggerFactory.getLogger(UserInfoServiceCommand.class);


    // 因为UserInfoServiceCommand对象需要new出来  ，所以UserInfoServiceImp不会自动注入
    private UserInfoServiceImp userInfoServiceImp = new UserInfoServiceImp();

    public UserInfoServiceCommand() {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("queryUserInfoGroupKey"))
        .andCommandKey(HystrixCommandKey.Factory.asKey("queryUserInfoKey"))
        .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
        .withExecutionIsolationThreadInterruptOnTimeout(true)
        .withExecutionTimeoutInMilliseconds(5000)));
    }

    @Override
    protected Observable<String> construct() {

        Observable<String> observable = Observable.unsafeCreate(new Observable.OnSubscribe<String>() {
            /*
             * Observable有三个关键的事件方法，分别为onNext，onCompleted，onError
             */

            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    if (!subscriber.isUnsubscribed()) {
                        logger.info("=========UserInfoServiceCommand当前端请求线程:{}=========", Thread.currentThread().getName());
                        subscriber.onNext(userInfoServiceImp.queryName());
                        subscriber.onNext(userInfoServiceImp.queryAddress());
                        subscriber.onCompleted();
                    }

                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.io());

        return observable;
    }

    @Override
    protected Observable<String> resumeWithFallback() {
        Observable<String> stringObservable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext("失败了！");
                    subscriber.onCompleted();
                }

            }
        }).subscribeOn(Schedulers.io());

        return stringObservable;
    }
}
